﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Ession;
using System.Threading.Tasks;

namespace PassGenerator
{
    public partial class frmPermutations : Form
    {
        public frmPermutations()
        {
            InitializeComponent();
            openFileDialog1.InitialDirectory = Application.StartupPath;
        }

        private FileStream fs;
        private string fileName;
        private int passLength;
        private Ession.Permutations<char> permutations;
        List<char> charactors;// = new List<char>();//字符集
        private void btnEs_Click(object sender, EventArgs e)
        {
            passLength = int.Parse(txtLength.Text);
            int count = 1;
            if (charactors == null)
            {
                charactors = File.ReadAllLines(txtDigits.Text).Select(
                  s =>
                  {
                      return s[0];
                  }
                  ).ToList();
            }
            if (charactors.Count - passLength < 0)
            {
                MessageBox.Show("密码位数太多");
            }
            for (int i = charactors.Count; i > charactors.Count - passLength; i--)
            {
                count *= i;
            }
            count *= (passLength + 2);
            if (count < 1024)
            {
                MessageBox.Show(count.ToString());
            }
            if (count > 1024 * 1024)
            {
                MessageBox.Show((count / 1024 / 1024).ToString() + " M");

            }
            else
            {
                if (count >= 1024)
                {
                    MessageBox.Show((count / 1024).ToString() + " K");
                }
            }
        }

        private void btnSS_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtDigits.Text = openFileDialog1.FileName;
                charactors = File.ReadAllLines(txtDigits.Text).Select(
                    s =>
                    {
                        return s[0];
                    }
                    ).ToList();

            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = saveFileDialog1.FileName;
                fileName = txtFilePath.Text;
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            fs = new FileStream(fileName, FileMode.Append);

            permutations = new Permutations<char>(charactors, passLength);
            Task.Factory.StartNew(
                ()=>
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        foreach (var permutation in permutations)
                        {
                            string s = new string(permutation.ToArray());
                            sw.WriteLine(s);
                        }
                    }
                    MessageBox.Show("done");
                }
                );
       

        }

        private void frmPermutations_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Save();
        }
    }
}
